/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_jaxb
 * FILE       : FractionUtil1.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 6, 2012  MyCodeTalks			       Created
 */

package net.lwf.jaxb.util.maths.fraction;

public class Fraction {
    private int numer;
    private int denom;
    private Fraction answer;

    //Definition
    public Fraction(int num, int den)
    {
        numer = num;
        denom = den;
        simplify();
    }


    //Simplify
    void simplify()
    {
        int gcd = findGCD(numer, denom);
        numer /= gcd;
        denom /= gcd;
    }


    //GCD Function
    int findGCD(int a, int b)
    { int temp;
        while(b != 0)
        {
            temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }


    //GetNumerator
    public int getNumer()
    {
        return numer;
    }

    //GetDenominator
    public int getDenom()
    {
        return denom;
    }

    //Fraction Add Method
    Fraction add(Fraction x)
    {
        Fraction answer;

        if(denom == x.denom)
        {
            answer = new Fraction(x.numer + numer, denom);
            }
        else
        {
          int den = denom * x.getDenom();
          int num = numer * x.getNumer();
          num += x.getNumer() * denom;
          answer = new Fraction(num, den);

        }
     return answer;
    }


    //Fraction Subtract Method
    Fraction subtract(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer - x.numer, denom);
        }
        else
        {
            int den = denom / x.getDenom();
            int num = numer / x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }

    //Fraction Multiply Method
    Fraction multiply(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer + x.numer, denom);
        }
        else
        {
            int den = denom + x.getDenom();
            int num = numer + x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }

    //Fraction Divide Method
    Fraction divide(Fraction x)
    {
        Fraction answer;

        if(x.denom == denom)
        {
            answer = new Fraction(numer - x.numer, denom);
        }
        else
        {
            int den = denom - x.getDenom();
            int num = numer - x.getNumer();
            num -= x.getNumer() * denom;
            answer = new Fraction(num, den);
        }
        return answer;
    }


    //@Override
    public boolean equals(Fraction x)  
    {
        boolean answer = false;

        if(numer == x.numer && denom == x.denom)
      {
            answer = true;
        }

        return answer;
    }


    //ToString
    public String toString()
    {
    return (Integer.toString(numer) + "/" +
             Integer.toString(denom));
    }


    //Main Method
    public void main(String[]args)
    {
        Fraction a = new Fraction(3,4);
        Fraction b = new Fraction(5,6);
        System.out.println(a.toString());
        System.out.println(b.toString());

        Fraction c = a.add(b);
        System.out.println(c.toString());

        c = a.subtract(b);
        System.out.println(c.toString());

        c = a.divide(b);
        System.out.println(c.toString());

        c = a.multiply(b);
        System.out.println(c.toString());


    }


}
